package com.ruoyi.client.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.client.service.ISysMedicalService;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.client.domain.SysBuyCar;
import com.ruoyi.client.service.ISysBuyCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购物车 Controller
 * 
 * @author ruoyi
 * @date 2022-09-23
 */
@RestController
@RequestMapping("/system/car")
public class SysBuyCarController extends BaseController
{
    @Autowired
    private ISysBuyCarService sysBuyCarService;

    @Autowired
    private ISysMedicalService sysMedicalService;

    /**
     * 查询购物车 列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysBuyCar sysBuyCar)
    {
        sysBuyCar.setUserId(SecurityUtils.getUserId());
        List<SysBuyCar> list = sysBuyCarService.selectSysBuyCarList(sysBuyCar);
        for (SysBuyCar item:list) {
            item.setSysMedical(sysMedicalService.selectSysMedicalByMedId(item.getMedId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出购物车 列表
     */
    @PreAuthorize("@ss.hasPermi('system:car:export')")
    @Log(title = "购物车 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBuyCar sysBuyCar)
    {
        List<SysBuyCar> list = sysBuyCarService.selectSysBuyCarList(sysBuyCar);
        ExcelUtil<SysBuyCar> util = new ExcelUtil<SysBuyCar>(SysBuyCar.class);
        util.exportExcel(response, list, "购物车 数据");
    }

    /**
     * 获取购物车 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:car:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return AjaxResult.success(sysBuyCarService.selectSysBuyCarByCarId(carId));
    }

    /**
     * 新增购物车 
     */
    @Log(title = "购物车 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBuyCar sysBuyCar)
    {
        sysBuyCar.setUserId(SecurityUtils.getUserId());
//        查询是否有此类商品

        List<SysBuyCar> list= sysBuyCarService.selectSysBuyCarList(sysBuyCar);
        if(list.isEmpty()){
            return toAjax(sysBuyCarService.insertSysBuyCar(sysBuyCar));
        }else {
//           存在则修改购物车数量数据
            sysBuyCar.setCarId(list.get(0).getCarId());
            return toAjax(sysBuyCarService.updateSysBuyCar(sysBuyCar));
        }
    }

    /**
     * 修改购物车 
     */
    @Log(title = "购物车 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBuyCar sysBuyCar)
    {
        return toAjax(sysBuyCarService.updateSysBuyCar(sysBuyCar));
    }

    /**
     * 删除购物车 
     */
    @Log(title = "购物车 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carId}")
    public AjaxResult remove(@PathVariable Long carId)
    {
        return toAjax(sysBuyCarService.deleteSysBuyCarByCarId(carId));
    }
}
